<!--
文件名：StudentFormDialog.vue
作者：教务系统开发团队
创建日期：2025-04-27
描述：学生表单对话框组件，用于新增和编辑学生信息
-->
<template>
  <el-dialog 
    :title="dialogType === 'create' ? '新增学生' : '编辑学生'" 
    :visible.sync="visible" 
    width="50%"
    @close="handleClose"
  >
    <el-form 
      ref="form" 
      :model="formData" 
      :rules="rules" 
      label-width="100px"
      label-position="right"
    >
      <el-form-item label="学号" prop="studentId">
        <el-input v-model="formData.studentId" placeholder="请输入学号"></el-input>
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input v-model="formData.name" placeholder="请输入姓名"></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="gender">
        <el-radio-group v-model="formData.gender">
          <el-radio label="M">男</el-radio>
          <el-radio label="F">女</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="出生日期" prop="birthDate">
        <el-date-picker 
          v-model="formData.birthDate" 
          type="date" 
          placeholder="选择日期"
          value-format="yyyy-MM-dd"
          style="width: 100%"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="班级" prop="className">
        <el-input v-model="formData.className" placeholder="请输入班级"></el-input>
      </el-form-item>
      <el-form-item label="校区" prop="campus">
        <el-input v-model="formData.campus" placeholder="请输入校区"></el-input>
      </el-form-item>
      <el-form-item label="入学日期" prop="enrollmentDate">
        <el-date-picker 
          v-model="formData.enrollmentDate" 
          type="date" 
          placeholder="选择日期"
          value-format="yyyy-MM-dd"
          style="width: 100%"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="formData.status" placeholder="请选择状态" style="width: 100%">
          <el-option label="在读" value="active"></el-option>
          <el-option label="已毕业" value="graduated"></el-option>
          <el-option label="已转学" value="transferred"></el-option>
          <el-option label="休学" value="suspended"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="联系电话" prop="phone">
        <el-input v-model="formData.phone" placeholder="请输入联系电话"></el-input>
      </el-form-item>
      <el-form-item label="家庭住址" prop="address">
        <el-input 
          v-model="formData.address" 
          type="textarea" 
          :rows="2" 
          placeholder="请输入家庭住址"
        ></el-input>
      </el-form-item>
      <el-form-item label="家长姓名" prop="parentName">
        <el-input v-model="formData.parentName" placeholder="请输入家长姓名"></el-input>
      </el-form-item>
      <el-form-item label="家长电话" prop="parentPhone">
        <el-input v-model="formData.parentPhone" placeholder="请输入家长电话"></el-input>
      </el-form-item>
    </el-form>
    
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取 消</el-button>
      <el-button type="primary" @click="handleSubmit">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { createStudent, updateStudent } from '@/api/student'

export default {
  name: 'StudentFormDialog',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    formData: {
      type: Object,
      default: () => ({
        studentId: '',
        name: '',
        gender: 'M',
        birthDate: '',
        className: '',
        campus: '',
        enrollmentDate: '',
        status: 'active',
        phone: '',
        address: '',
        parentName: '',
        parentPhone: ''
      })
    },
    dialogType: {
      type: String,
      default: 'create'
    }
  },
  data() {
    return {
      rules: {
        studentId: [
          { required: true, message: '请输入学号', trigger: 'blur' },
          { pattern: /^\d{10}$/, message: '学号必须为10位数字', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
        ],
        className: [
          { required: true, message: '请输入班级', trigger: 'blur' }
        ],
        campus: [
          { required: true, message: '请输入校区', trigger: 'blur' }
        ],
        enrollmentDate: [
          { required: true, message: '请选择入学日期', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    /**
     * 处理表单提交
     */
    handleSubmit() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          try {
            if (this.dialogType === 'create') {
              await createStudent(this.formData)
              this.$message.success('新增学生成功')
            } else {
              await updateStudent(this.formData.id, this.formData)
              this.$message.success('更新学生成功')
            }
            this.$emit('refresh')
            this.$emit('update:visible', false)
          } catch (error) {
            console.error('保存学生信息失败:', error)
            this.$message.error('保存学生信息失败')
          }
        }
      })
    },
    
    /**
     * 处理对话框关闭
     */
    handleClose() {
      this.$refs.form.resetFields()
      this.$emit('update:visible', false)
    }
  },
  watch: {
    visible(val) {
      if (val) {
        this.$nextTick(() => {
          this.$refs.form.clearValidate()
        })
      }
    }
  }
}
</script>

<style scoped>
.el-form-item {
  margin-bottom: 22px;
}
</style>